package com.taotao.manage.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.service.ItemCatService;
import com.taotao.manage.service.ItemService;

@Controller
@RequestMapping("item")
public class ItemController {

	@Autowired
	private ItemService itemService;
	
	@Autowired
	private ItemCatService itemCatService;

	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc") String desc) {

		try {
			if (StringUtils.isEmpty(item.getTitle())) {
				return ResponseEntity.badRequest().build();
			}
			this.itemService.insertItemAndItemDesc(item, desc);

			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemListByPage(@RequestParam(value = "page",defaultValue = "1")Integer pageNum,
			@RequestParam(value = "rows",defaultValue = "30")Integer pageSize) {
		try {
			PageInfo<Item> pageInfo = itemService.queryPageListByOrderBy("updated desc", pageNum, pageSize);
			if (CollectionUtils.isEmpty(pageInfo.getList())) {
				return ResponseEntity.notFound().build();
			}
			//添加cname字段
		/*	ArrayList<ItemWithCat> arrayList = new ArrayList<>();
			for (Item item : pageInfo.getList()) {
				ItemCat itemCat = itemCatService.queryById(item.getCid());
				ItemWithCat itemWithCat = new ItemWithCat();
				BeanUtils.copyProperties(itemWithCat, item);
				
				
				itemWithCat.setCname(itemCat.getName());
				arrayList.add(itemWithCat);
			}*/
			
			EasyUIResult result = new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(Item item, @RequestParam("desc") String desc) {

		try {
			if (StringUtils.isEmpty(item.getTitle())) {
				return ResponseEntity.badRequest().build();
			}
			this.itemService.updateItemAndItemDesc(item, desc);
			
		

			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ResponseEntity<Void> deleteItemByIds(@RequestParam("ids") List<Long> ids){
		for (Long long1 : ids) {
			Item item = new Item();
			item.setId(long1);
			item.setStatus(2);
			itemService.updateSelective(item);
		}
		return ResponseEntity.ok().build();
		
	}
}
